﻿#region Using

using System;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNgine.Services;
using XNgine.Util;

#endregion

namespace XNgine.ScreenManagement.StandardScreens
{
    /// <summary>  
    /// A popup message box screen, used to display "are you sure?"  
    /// confirmation messages.  
    /// </summary>  
    public class MessageBox : PopupScreen
    {
        public const string SCREEN_NAME = "XNgine.MessageBox";

        public static SpriteFont Font { get; set; }

        private Texture2D backgroundTexture;
        private readonly string _message;

        public event EventHandler<EventArgs> Accepted;
        public event EventHandler<EventArgs> Cancelled;

        /// <summary>  
        /// Constructor automatically includes the standard "A=ok, B=cancel"  
        /// usage text prompt.  
        /// </summary>  
        public MessageBox(string message)
            : this(message, true)
        {
        }

        /// <summary>  
        /// Constructor lets the caller specify whether to include the standard  
        /// "A=ok, B=cancel" usage text prompt.  
        /// </summary>  
        public MessageBox(string message, bool includeUsageText)
            : base(SCREEN_NAME)
        {
            const string usageText = "\nSpace, Enter = OK" +
                                     "\nEsc = Cancel";

            if (includeUsageText)
                _message = message + usageText;
            else
                _message = message;

            XEngine.Instance.GetService<IKeyboardDevice>().KeyPressed += MessageBox_KeyPressed;
        }

        private void MessageBox_KeyPressed(Collection<Keys> keys)
        {
            if (!IsInputAllowed) return;

            if (keys.Contains(Keys.Enter) || keys.Contains(Keys.Space))
            {
                // Raise the accepted event, then exit the message box.  
                if (Accepted != null)
                    Accepted(this, EventArgs.Empty);

                Close();
            }
            if (keys.Contains(Keys.Escape))
            {
                // Raise the cancelled event, then exit the message box.  
                if (Cancelled != null)
                    Cancelled(this, EventArgs.Empty);

                Close();
            }
        }

        public override void LoadContent()
        {
            if (ContentLoaded) return;
            backgroundTexture = XEngine.Instance.Content.Load<Texture2D>("Textures/gradient");
            Font = XEngine.Instance.Content.Load<SpriteFont>("Fonts/menufont");
            ContentLoaded = true;
        }

        public override void Update()
        {
        }

        public override void Draw()
        {
            // Darken down any other screens that were drawn beneath the popup.  
            GraphicsUtil.FadeBackBufferToBlack(100);

            // Center the message text in the viewport.  
            Viewport viewport = XEngine.Instance.GraphicsDevice.Viewport;
            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
            Vector2 textSize = Font.MeasureString(_message);
            Vector2 textPosition = (viewportSize - textSize)/2;

            // The background includes a border somewhat larger than the text itself.  
            const int hPad = 32;
            const int vPad = 16;

            Rectangle backgroundRectangle = new Rectangle((int) textPosition.X - hPad,
                                                          (int) textPosition.Y - vPad,
                                                          (int) textSize.X + hPad*2,
                                                          (int) textSize.Y + vPad*2);

            // Fade the popup alpha during transitions.  
            Color color = Color.White;

            XEngine.Instance.SpriteBatch.Begin();

            // Draw the background rectangle.  
            XEngine.Instance.SpriteBatch.Draw(backgroundTexture, backgroundRectangle, color);

            // Draw the message box text.  
            XEngine.Instance.SpriteBatch.DrawString(Font, _message, textPosition, color);

            XEngine.Instance.SpriteBatch.End();
        }
    }
}